// myself/pages/message-center/message-center.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    cdnPath: app.globalData.CDNPATH,
    isLoadMore: false,
    isGetMessageList:false,
    messageList:[],
    page: {
      currentPage: 1,
      pageSize: 10,
      total: 0,
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getMessageList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},
  toMessageDatil(e){
    console.log(e)
    let id = e.currentTarget.dataset.id;
   
    wx.navigateTo({
      url: `/myself/pages/message-detail/message-detail?id=${id}`,
    })
  },
  //滚动到底部触发
  scrolltolower() {
    console.log("scrolltolower");
    this.loadMore();
  },
  loadMore: function () {
    const { currentPage, total } = this.data.page;
    if (
      total > this.data.messageList.length &&
      this.data.isGetMessageList === false
    ) {
      this.setData({
        "page.currentPage": currentPage + 1,
        isLoadMore: true,
      });
      this.getMessageList();
    }
  },
  changeTimeFormat(timestamp){
    let newTimes = timestamp.replace(/-/g,  "/")
    let date = new Date(Date.parse(newTimes))
    let nowDate = new Date()
    let days = this.getDaysBetween(nowDate.getFullYear()+'/'+(nowDate.getMonth()+1)+'/'+nowDate.getDate(''), newTimes.substr(0,10))
    let result = ''
    if(days>= 2){
      result = (date.getMonth() +1) + '月' + date.getDate() + '日'
    }else if(days >= 1 && days<2){
      result = '昨天'
    }else{
      let minutes = ('00' + date.getMinutes() ).slice(-2)
      if(date.getHours() < 13){
        let hour = ('00' + date.getHours() ).slice(-2)
        result = '上午 ' + hour +':'+minutes
      }else{
        let hour = ('00' + (date.getHours()-12) ).slice(-2)
        result = '下午 ' + hour +':'+minutes
      }
    }
    
    return result
  },
  getDaysBetween (date1,date2) {
    let startDate = Date.parse(date2);
    let endDate = Date.parse(date1);
    let days=(endDate - startDate)/(1*24*60*60*1000);
   return  days;
  },
  async getMessageList() {
    console.log('this.data.isGetMessageList')
    if (this.data.isGetMessageList) return;

    let {  page, messageList } = this.data;
    const { currentPage, pageSize } = page;
    try {
      this.data.isGetMessageList = true;
      let res = await app.api.post({
        url: "broadcast_messageCenterList",
        data: { currentPage, pageSize},
      });

      let arr = [];
      let list = [];
      arr = res.list;
      arr.forEach((item,index)=>{
        arr[index].publishTime = this.changeTimeFormat(item.issueTime)
      })
      list = messageList
      if (this.data.isLoadMore) {
        console.log("执行加载");
        list.push(...arr);
      } else {
        console.log("执行加载2");
        list = arr;
      }
      this.setData({
        messageList: list,
        isLoadMore: false,
        'page.total': Number(res.total)
      });

      this.data.isGetMessageList = false;
    } catch (e) {
      this.data.isGetMessageList = false;
    }
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
